package com.zds.support.gilde

import android.content.Context
import android.graphics.Bitmap
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.RecyclerView
import com.zds.support.R

class ImageLoaderManager {
    private lateinit var imageLoaderStrategy: ImageLoaderStrategy
    var baseImageUrl = ""

    private object Holder {
        val INSTANCE = ImageLoaderManager()
    }

    companion object {
        @JvmStatic
        val instance: ImageLoaderManager by lazy { Holder.INSTANCE }
    }

    private constructor() {

    }

    fun init(context: Context, imageLoaderStrategy: ImageLoaderStrategy) {
        this.imageLoaderStrategy = imageLoaderStrategy
        imageLoaderStrategy.init(context)
    }

    fun load(path: String) {
        imageLoaderStrategy.load(path)
    }

    fun showImage(imageView: ImageView, path: String) {
        imageLoaderStrategy.showImage(imageView, path)
    }

    fun showImage(imageView: ImageView, path: String,
                  @DrawableRes placeholderResId: Int,
                  @DrawableRes failureResId: Int) {
        imageLoaderStrategy.showImage(imageView, path, placeholderResId, failureResId)
    }

    fun showImage(imageView: ImageView, path: String,
                  @DrawableRes placeholderResId: Int,
                  @DrawableRes failureResId: Int,
                  onSuccessListener: (path: String) -> Unit,
                  onFailListener: (e: Exception?) -> Unit) {
        imageLoaderStrategy.showImage(imageView, path, placeholderResId, failureResId,
            onSuccessListener, onFailListener)
    }

    fun showImageCorner(imageView: ImageView, path: String,
                        @DrawableRes placeholderResId: Int,
                        @DrawableRes failureResId: Int,
                        cornerSize: Int) {
        imageLoaderStrategy.showImageCorner(imageView, path, placeholderResId, failureResId, cornerSize)
    }

    fun loadGif(imageView: ImageView, path: String) {
        imageLoaderStrategy.showImage(imageView, path, true, R.color.transparent, R.color.transparent)
    }

    fun showImage(imageView: ImageView, path: String, roundSize: Int,
                  @DrawableRes placeholderResId: Int,
                  @DrawableRes failureResId: Int) {

    }

    fun getBitmap(path: String, onGetBitmapListener: (bitmap: Bitmap) -> Unit) {
        imageLoaderStrategy.getBitmap(path, onGetBitmapListener)
    }

    fun getBitmapSync(path: String): Bitmap {
        return imageLoaderStrategy.getBitmapSync(path)
    }


    fun clearMemory() {
        try {
            imageLoaderStrategy.clearMemory()
        } catch (e: Exception) {
        }
    }

    fun trimMemory(level: Int) {
        try {
            imageLoaderStrategy.trimMemory(level)
        } catch (e: Exception) {
        }
    }

    fun resumeRequests() {
        try {
            imageLoaderStrategy.resumeRequests()
        } catch (e: Exception) {
        }
    }

    fun pauseRequests() {
        try {
            imageLoaderStrategy.pauseRequests()
        } catch (e: Exception) {
        }
    }

    /**
     * RecyclerView根据滚动状态自动暂停/恢复加载图片OnScrollListener
     */
    fun getRecyclerViewAutoPauseOrResumeScrollListener(): RecyclerView.OnScrollListener {
        return object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    resumeRequests()
                } else {
                    pauseRequests()
                }
            }
        }
    }
}